/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.beans;

import com.entity.ConfigFile;
import com.entity.Host;
import com.entity.Service;
import com.logic.HostAdmin;
import com.icesoft.faces.component.ext.RowSelectorEvent;
import java.util.HashMap;
import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;

/**
 *
 * @author root
 */
@ManagedBean
public class AdminHostBean {

    private String popupTitle = "Agregar Servicio";
    //propiedades para crear nuevos servicios
    private String name;
    private String status;
    private String start;
    private String stop;
    private String restart;
    //mapa para representar la seleccion de filas de la tabla de servicios
    private HashMap<Service, Boolean> selectedServiceRow = new HashMap<Service, Boolean>();
    //mapa para representar la seleccion de filas de la tabla de archivos
    private HashMap<ConfigFile, Boolean> selectedFileRow = new HashMap<ConfigFile, Boolean>();
    //indice de la tabla seleccionado
    private static int selectedIndex = -1;

    public Host getHost() {
        return HostAdmin.getInstance().getHost();
    }

    public void clearFields() {
        name = "";
        status = "";
        start = "";
        stop = "";
        restart = "";
    }

    public void restartListener(ActionEvent e) {
        HostAdmin.getInstance().restartHost();
    }

    public void adminDaemon(ActionEvent e) {
        int signal = Integer.parseInt(e.getComponent().getId().replace("a", "").trim());
        HostAdmin.getInstance().adminDaemon(signal, selectedIndex);
    }

    public void addService(ActionEvent e) {
        if (!(name.equals("") && status.equals("") && start.equals("") && restart.equals("") && stop.equals(""))) {
            HostAdmin.getInstance().addItem(0, name, start, stop, restart, status);
        }
    }

    public void loadItemInfo() {
        Service serv = HostAdmin.getInstance().getHost().getServiceList().get(selectedIndex);
        name = serv.getDaemonName();
        status = serv.getStatusCommand();
        start = serv.getStartCommand();
        stop = serv.getStopCommand();
        restart = serv.getRestartCommand();
    }

    public void deleteService(ActionEvent e) {
        HostAdmin.getInstance().removeItem(0, selectedIndex);
    }

    public void serviceSelectionListener(RowSelectorEvent event) {
        selectedIndex = event.getRow();
        selectedServiceRow = new HashMap<Service, Boolean>();
        selectedServiceRow.put(HostAdmin.getInstance().getHost().getServiceList().get(selectedIndex), (Boolean) event.isSelected());
        System.out.println(selectedIndex);
    }

    public void fileSelectionListener(RowSelectorEvent event) {
        selectedIndex = event.getRow();
        selectedFileRow = new HashMap<ConfigFile, Boolean>();
        selectedFileRow.put(HostAdmin.getInstance().getHost().getFileList().get(selectedIndex), (Boolean) event.isSelected());
        System.out.println(selectedIndex);
    }

    public int getSelectedIndex() {
        return selectedIndex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getStart() {
        return start;
    }

    public void setStart(String start) {
        this.start = start;
    }

    public String getStop() {
        return stop;
    }

    public void setStop(String stop) {
        this.stop = stop;
    }

    public String getRestart() {
        return restart;
    }

    public void setRestart(String restart) {
        this.restart = restart;
    }

    public HashMap<Service, Boolean> getSelectedServiceRow() {
        return selectedServiceRow;
    }

    public void setSelectedServiceRow(HashMap<Service, Boolean> selectedServiceRow) {
        this.selectedServiceRow = selectedServiceRow;
    }

    public HashMap<ConfigFile, Boolean> getSelectedFileRow() {
        return selectedFileRow;
    }

    public void setSelectedFileRow(HashMap<ConfigFile, Boolean> selectedFileRow) {
        this.selectedFileRow = selectedFileRow;
    }

    public String getPopupTitle() {
        return popupTitle;
    }

    public void setPopupTitle(String popupTitle) {
        this.popupTitle = popupTitle;
    }
    
    
}
